var TcInfoDlg = {
    tcInfoData : {},
    zTreeInstance : null,
    validateFields: {
        tcName: {
            validators: {
                notEmpty: {
                    message: 'tc姓名不能为空'
                },
                stringLength: {
                    max: 20,
                    message: 'tc姓名名称长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
    }
};

TcInfoDlg.clearData = function() {
    this.tcInfoData = {};
}

TcInfoDlg.set = function(key, val) {
    this.tcInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

TcInfoDlg.get = function(key) {
    return $("#" + key).val();
}

TcInfoDlg.close = function() {
    parent.layer.close(window.parent.Tc.layerIndex);
}

TcInfoDlg.onClickTc = function(e, treeId, treeNode) {
    $("#pName").attr("value", TcInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
}

TcInfoDlg.showTcSelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentTcMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

TcInfoDlg.hideTcSelectTree = function() {
    $("#parentTcMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

TcInfoDlg.collectData = function() {
    this.set('id').set('tcName').set('tcPhone').set('tcSex').set('tcBirthday').set('remarks');
}

TcInfoDlg.validate = function () {
    $('#tcInfoForm').data("bootstrapValidator").resetForm();
    $('#tcInfoForm').bootstrapValidator('validate');
    return $("#tcInfoForm").data('bootstrapValidator').isValid();
}

TcInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/tc/add", function(data){
        Feng.success("添加成功!");
        window.parent.Tc.table.refresh();
        TcInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tcInfoData);
    ajax.start();
}

TcInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/tc/update", function(data){
        Feng.success("修改成功!");
        window.parent.Tc.table.refresh();
        TcInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tcInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentTcMenu" || $(
            event.target).parents("#parentTcMenu").length > 0)) {
        TcInfoDlg.hideTcSelectTree();
    }
}

TcInfoDlg.initData = function () {
    var seletTcSex = $('#itcSex').val();
    if ($('#id').val() != "") {
        $("#tcSex option[value='" + seletTcSex + "']").attr("selected", "selected");
    }
}

$(function() {
    Feng.initValidator("tcInfoForm", TcInfoDlg.validateFields);
    TcInfoDlg.initData();
});